<?php
/**
 * GlobalExceptionHandler
 *
 * @Description 描述
 * @Author look
 * @CreateTime 2022/2/22
 */

namespace app\common\handle;

use app\common\exception\ApiException;
use app\common\traits\ResultData;
use think\exception\Handle;
use think\exception\ValidateException;
use think\Response;
use Throwable;

/**
 * Class GlobalExceptionHandler
 */
class GlobalExceptionHandle extends Handle{

    use ResultData;

    public function render($request, Throwable $e): Response {
        if ($e instanceof ApiException) {
            return $this->error(error: $e->getCode(), msg: $e->getMessage());
        } else if ($e instanceof ValidateException) {
            return $this->error($e->getMessage());
        }
        return parent::render($request, $e);
    }

}